package com.qb.mobile;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

import com.qb.mobile.R;
import com.qb.mobile.core.actionbar.Action;
import com.qb.mobile.core.actionbar.ActionBar;
import com.qb.mobile.core.actionbar.IntentAction;
import com.qb.mobile.model.Contact;

import android.view.View;

/**
 * @author <a href="mailto:dangcongthanhtrung@gmail.com">dangtrung</a>
 * 
 * PhonebookDetail.java class
 *
 */
public class PhonebookDetail extends Activity {

	private Contact phoneBook;
	
	/**
	 * Called when the activity is first created.
	 */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.phone_book_detail);
		setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
		getIntentData();
		initActionBar();
	}

	private void initActionBar() {
		final ActionBar actionBar = (ActionBar) findViewById(R.id.actionbar);
		actionBar.setTitle(getResources().getString(R.string.activity_title_detail));
		
		actionBar.setHomeAction(new IntentAction(this, createHomeIntent(this),
				R.drawable.ic_home_default));
		
		Action smsAction = new IntentAction(this, createSmsIntent(),
				R.drawable.ic_mail_default);
		actionBar.addAction(smsAction);

		Action shareAction = new IntentAction(this, createCallIntent(),
				R.drawable.ic_phone_default);
		actionBar.addAction(shareAction);
		
		Action deleteAction = new IntentAction(this, createDeleteIntent(),
				R.drawable.ic_delete_default, true, false);
		actionBar.addAction(deleteAction);
	}

	private void getIntentData() {
		Intent intent = getIntent();
		phoneBook = (Contact) intent.getSerializableExtra("Phonebook");
		setDataComponent();
	}

	private void setDataComponent() {
		TextView nameTextView = (TextView) findViewById(R.id.phone_book_detail_contact_name);
		TextView phoneTextView = (TextView) findViewById(R.id.phone_book_detail_contact_phone);
		ImageView imageView = (ImageView)findViewById(R.id.contact_picture);
		
		nameTextView.setText(phoneBook.getName());
		phoneTextView.setText(phoneBook.getMobilePhone());
		if(phoneBook.getPhoto() != null){
			byte[] photoBlob = phoneBook.getPhoto();
			Bitmap photoBitmap = BitmapFactory.decodeByteArray(photoBlob, 0,
					photoBlob.length);
			imageView.setImageBitmap(photoBitmap);
		}
	}

	private Intent createHomeIntent(Context context) {
		Intent intent = new Intent(context, PhonebookList.class);
		intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
		setResult(RESULT_CANCELED, intent);
		return intent;
	}

	public void doClose(View view) {
		setResult(RESULT_CANCELED);
		this.finish();
	}

	private Intent createSmsIntent() {
		String url = "sms:" + phoneBook.getMobilePhone();
		return new Intent(Intent.ACTION_VIEW, Uri.parse(url));
	}

	private Intent createCallIntent() {
		String url = "tel:" + phoneBook.getMobilePhone();
		return new Intent(Intent.ACTION_CALL, Uri.parse(url));
	}

	private Intent createDeleteIntent() {
		Intent intent = new Intent();
		intent.putExtra("Id", phoneBook.getId());
		return intent;
	}
}